import { ITimer } from "../../api/timer/ITimer";
import { TimerUtils } from "../../api/timer/TimerUtils";
import { E_TimeConfig } from "../defs/com/E_TimeConfig";
import { E_StageGlobal } from "../defs/stage/E_StageGlobal";
import { CDStageGlobal } from "../excel/CDStageGlobal";
import { CEStage } from "../excel_extend/CEStage";
import { Player } from "../struct/Player";


/**
 * 数据-试练塔
 */
export abstract class SDTrainTower {


    /**
     * 下次需要请求更新的时间
     * @param player 
     */
    public static nextUpdateTime(player: Player): number {
        const nextDay = TimerUtils.getDayZeroTime(ITimer.getServerTime() + E_TimeConfig.Day);
        return nextDay;
    }


    /**
     * 免费刷新次数上限
     * @param player 
     */
    public static getBuffRefreshFreeMax(player: Player): number {
        const config = CDStageGlobal.getData(E_StageGlobal.TrainTowerRefreshFree);
        if (config) {
            return config.value;
        }
        return 0;
    }


    /**
     * 广告刷新次数上限
     * @param player 
     */
    public static getBuffRefreshADMax(player: Player): number {
        const config = CDStageGlobal.getData(E_StageGlobal.TrainTowerMaxRefresh);
        if (config) {
            return config.value - this.getBuffRefreshFreeMax(player);
        }
        return 0;
    }


    /**
     * 是否跨天
     * @param player 
     * @returns 
     */
    public static isSkipDay(player: Player): boolean {
        if (player.trialTower.stageId !== 0) {
            const d1 = TimerUtils.getDayZeroTime(player.trialTower.buff.refreshTime);
            const d2 = TimerUtils.getDayZeroTime(ITimer.getServerTime());
            if (d1 < d2) {
                return true;
            }
        }
        return false;
    }


    /**
     * 当前层数
     * @param stageId 
     */
    public static getStageIndex(stageId: number): number {
        const ks = CEStage.trialTowerStage.getKeys();
        const idx = ks.indexOf(stageId);
        return idx >= 0 ? idx : 0;
    }


    /**
     * 每日奖励是否有效
     * @param player 
     * @returns 
     */
    public static isDailyBoxEnable(player: Player): boolean {
        if (player.trialTower.stageId !== 0) {
            const d1 = TimerUtils.getDayZeroTime(player.trialTower.dailyRewardGetTime);
            const d2 = TimerUtils.getDayZeroTime(ITimer.getServerTime());
            if (d1 < d2) {
                return true;
            }
        }
        return false;
    }




}
